/**
 * 
 */
package org.vsg.common.jobscheduler.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.vsg.common.jobscheduler.domain.QuartzTrigger;
import org.vsg.common.jobscheduler.persistence.ScheduleJobDao;
import org.vsg.common.jobscheduler.service.JobService;
import org.vsg.common.jobscheduler.vo.Argument;
import org.vsg.common.jobscheduler.vo.JobInfo;
import org.vsg.common.jobscheduler.vo.TriggerJob;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author vison
 *
 */
public class JobServiceImpl implements JobService,InitializingBean {
	
	@javax.annotation.Resource
	private ScheduleJobDao scheduleJobDao;
	
	private List<JobInfo> list;
	
	/**
	 * get all job info from module file
	 */
	private Resource moduleJobsFile;

	public Resource getModuleJobsFile() {
		return moduleJobsFile;
	}

	public void setModuleJobsFile(Resource moduleJobsFile) {
		this.moduleJobsFile = moduleJobsFile;
	}


	public ScheduleJobDao getScheduleJobDao() {
		return scheduleJobDao;
	}

	public void setScheduleJobDao(ScheduleJobDao scheduleJobDao) {
		this.scheduleJobDao = scheduleJobDao;
	}

	/* (non-Javadoc)
	 * @see org.vsg.common.jobscheduler.service.JobService#listModuleAvaiRegJobs()
	 */
	@Override
	public List<JobInfo> listModuleAvaiRegJobs() {
		// TODO Auto-generated method stub
		return list;
	}

	
	@Override
	public List<Argument> getJobInfoArgumentByCode(String code) {
		// TODO Auto-generated method stub
		JobInfo jobInfo = null;
		List<Argument> args = new Vector<Argument>();
		for (Iterator<JobInfo> jobInfoIter = list.iterator(); jobInfoIter.hasNext() ; ) {
			jobInfo = jobInfoIter.next();
			if (jobInfo.getCode().equals(code)) {
				args = jobInfo.getArguments();
				break;
			}
		}
		return args;
	}
	

	
	@Override
	public JobInfo getModuleAvaiRegJob(String code) {
		// TODO Auto-generated method stub
		JobInfo jobInfo = null;
		for (Iterator<JobInfo> jobInfoIter = list.iterator(); jobInfoIter.hasNext() ; ) {
			jobInfo = jobInfoIter.next();
			if (jobInfo.getCode().equals(code)) {
				break;
			}
		}
		return jobInfo;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

		XStream xstream = new XStream(new DomDriver());
		
		xstream.alias("job-list", List.class);
		xstream.alias("arguments", List.class);
		xstream.alias("arg", Argument.class);
		xstream.alias("job-info", JobInfo.class);
		
		xstream.aliasField("type", Argument.class, "type");
		//xstream.aliasField("date", Argument.class, "date");
		
		list = (List<JobInfo>) xstream.fromXML( moduleJobsFile.getInputStream() );

	}

	@Override
	public List<TriggerJob> listAllScheduledJobsByGroup(String groupName) {
		// TODO Auto-generated method stub
		List<TriggerJob> triggerList = new ArrayList<TriggerJob>();

		for (Iterator<QuartzTrigger> triggerIter = scheduleJobDao.listQuartzTriggerByGroup(groupName).iterator() ; triggerIter.hasNext() ; ) {
			triggerList.add( new TriggerJob(triggerIter.next())  );
		}
		
		return triggerList;
	}
	
	

}
